package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorArticleNotificationPatientDTO;
import com.pshcp.domain.model.DoctorArticleNotificationPatient;
import com.pshcp.domain.model.DoctorPatientGroupUser;
import com.pshcp.mapper.DoctorArticleNotificationPatientMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医生文章信息提醒患者表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorArticleNotificationPatientService {

    @Autowired
    private DoctorArticleNotificationPatientMapper doctorArticleNotificationPatientMapper;

    public void batchSaveDoctorArticleNotificationPatient(List<DoctorArticleNotificationPatient> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Map<String, List<DoctorArticleNotificationPatient>> infoMap = new HashMap<>();
        infoMap.put("infoMap", list);
        doctorArticleNotificationPatientMapper.batchSaveDoctorArticleNotificationPatient(infoMap);
    }

    public List<DoctorArticleNotificationPatient> getDoctorArticleNotificationPatientList(Long doctorArticleId){
        Example example = new Example(DoctorArticleNotificationPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorArticleId", doctorArticleId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorArticleNotificationPatient> list = doctorArticleNotificationPatientMapper.selectByExample(example);
        return list;
    }

    public List<DoctorArticleNotificationPatient> getDoctorArticleNotificationPatientList(Long doctorArticleId,List<Long>patientIds){
        Example example = new Example(DoctorArticleNotificationPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorArticleId", doctorArticleId);
        criteria.andIn("patientId", patientIds);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorArticleNotificationPatient> list = doctorArticleNotificationPatientMapper.selectByExample(example);
        return list;
    }

    public PageList<DoctorArticleNotificationPatient> list(DoctorArticleNotificationPatientDTO doctorArticleNotificationPatientDTO){
        if(doctorArticleNotificationPatientDTO.getPageNum() != null && doctorArticleNotificationPatientDTO.getPageSize() != null){
            PageHelper.startPage(doctorArticleNotificationPatientDTO.getPageNum(), doctorArticleNotificationPatientDTO.getPageSize());
        }
        Example example = new Example(DoctorArticleNotificationPatient.class);
        Example.Criteria criteria = example.createCriteria();
        if(doctorArticleNotificationPatientDTO.getStatus() != null){
            criteria.andEqualTo("status", doctorArticleNotificationPatientDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorArticleNotificationPatient> list = doctorArticleNotificationPatientMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorArticleNotificationPatient> select(DoctorArticleNotificationPatient doctorArticleNotificationPatient){
        return doctorArticleNotificationPatientMapper.select(doctorArticleNotificationPatient);
    }

    public int insert(DoctorArticleNotificationPatient doctorArticleNotificationPatient){
        doctorArticleNotificationPatient.setId(SystemUtil.getId());
        doctorArticleNotificationPatient.setStatus(CommonStatusEnum.INIT.getCode());
        doctorArticleNotificationPatient.setCreatedTime(new Date());
        return doctorArticleNotificationPatientMapper.insert(doctorArticleNotificationPatient);
    }

    public int update(DoctorArticleNotificationPatient doctorArticleNotificationPatient){
        doctorArticleNotificationPatient.setUpdateTime(new Date());
        return doctorArticleNotificationPatientMapper.updateByPrimaryKeySelective(doctorArticleNotificationPatient);
    }

    public DoctorArticleNotificationPatient findById(Long id){
        return doctorArticleNotificationPatientMapper.selectByPrimaryKey(id);
    }

    public List<DoctorArticleNotificationPatient> findByIds(List<Long> ids){
        Example example = new Example(DoctorArticleNotificationPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorArticleNotificationPatientMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DoctorArticleNotificationPatient.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorArticleNotificationPatient doctorArticleNotificationPatient = new DoctorArticleNotificationPatient();
        doctorArticleNotificationPatient.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorArticleNotificationPatientMapper.updateByExampleSelective(doctorArticleNotificationPatient, example);
    }

}
